package com.yunji.storage.pattern;

import com.yunji.common.enums.common.ErrorEnum;
import com.yunji.common.enums.common.CacheCodeExtend;
import com.yunji.common.utils.DecimalUtils;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.storage.bo.AllocateSelectQueryBO;
import com.yunji.storage.domain.AllocateTicketDO;
import com.yunji.storage.domain.StockDO;
import com.yunji.storage.enums.AllocateStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.math.BigDecimal;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.storage.pattern
 * @Title: AllocateInteract
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-08-22 15:56:38      V1.0        HuaAo       新建类
 */
@Component
public class AllocateInteract extends AbstractStorage {


    @Override
    protected void addStorage(StockDO stock) {
        BigDecimal actualQuantity = stock.get(OPERATE_NUMS);
        // 调拨入库是操作可用库存
        logs.info("【调拨库存处理】新增库存:{}", actualQuantity);

        StockDO update = new StockDO();
        update.setBelongId(stock.getBelongId());
        update.setProductId(stock.getProductId());
        update.setUsableStock(actualQuantity);
        update.setAllStock(actualQuantity);

        Assert.isTrue(stockService.updateStock(update), "入库失败");
    }

    @Override
    protected void subtractStock(StockDO stock) {
        BigDecimal actualQuantity = stock.get(OPERATE_NUMS);
        logs.info("【调拨库存处理】扣减库存:{}", actualQuantity);

        BigDecimal usableStock = stock.getUsableStock();
        logs.info("【调拨库存处理】扣减库存前的可用库存:{}", usableStock);

        StockDO update = new StockDO();
        update.setBelongId(stock.getBelongId());
        update.setProductId(stock.getProductId());
        update.setAllStock(actualQuantity.negate());
        update.setUsableStock(actualQuantity.negate());

        Assert.isTrue(!DecimalUtils.more(actualQuantity, usableStock), "可用库存不足");
        Assert.isTrue(stockService.updateStock(update), "出库失败");
    }

    @Override
    protected void backStock(StockDO stock) {
        logs.info("【调拨库存处理】返还库存:{}", stock);
        BigDecimal actualQuantity = stock.get(OPERATE_NUMS);

        StockDO update = new StockDO();
        update.setBelongId(stock.getBelongId());
        update.setProductId(stock.getProductId());
        update.setUsableStock(actualQuantity);
        update.setAllStock(actualQuantity);

        Assert.isTrue(stockService.updateStock(update), "入库失败");
    }

    @Override
    protected void createBackStock(StockDO stock) {
        logs.info("【调拨库存处理】新增返还库存:{}", stock);
        BigDecimal actualQuantity = stock.get(OPERATE_NUMS);

        StockDO create = new StockDO();
        create.setBelongId(stock.getBelongId());
        create.setProductId(stock.getProductId());
        create.setUsableStock(actualQuantity);
        create.setAllStock(actualQuantity);

        Assert.isTrue(stockService.create(create), "新增失败");
    }

    @Override
    public void updateTicket(Long businessId) {
        logs.info("【调拨状态更改】调拨ID:{}", businessId);

        AllocateTicketDO update = new AllocateTicketDO();
        update.setAllocateId(businessId);
        update.setAllocateStatus(AllocateStatus.FINISH.getCode());
        Assert.isTrue(allocateTicketService.update(update), "修改失败");
    }

    @Override
    public void checkTicket(String businessCode) {
        logs.info("【校验调拨单】调拨单号:{}", businessCode);

        AllocateTicketDO vo = allocateTicketService.select(new AllocateSelectQueryBO(businessCode));
        Assert.isTrue(BeanUtils.isNotNull(vo), ErrorEnum.BUSINESS_812016.getText());

        redisCache.set(CacheCodeExtend.PARAM_001002.getInfo(), vo.getAllocateId());
    }


}